package com.qsmx.qingqiuticket.app.delegate.back;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.qsmx.core.delegates.AtmosDelegate;
import com.qsmx.core.util.StringUtil;
import com.qsmx.core.util.storage.AtmosPreference;
import com.qsmx.qingqiuticket.app.R;
import com.qsmx.qingqiuticket.app.databinding.DelegatePasswordBinding;
import com.qsmx.qingqiuticket.app.event.CheckPrintEvent;
import com.qsmx.qingqiuticket.app.fusion.FusionPre;

import org.greenrobot.eventbus.EventBus;

import java.util.Objects;

public class PasswordDelegate extends AtmosDelegate {

    private DelegatePasswordBinding mBinding;

    @Override
    public Object setLayout() {
        return R.layout.delegate_password;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

        mBinding = DelegatePasswordBinding.bind(rootView);
        onClick();
        if (StringUtil.isEmpty(AtmosPreference.getCustomStringPre(FusionPre.BACK_PASSWORD))) {
            AtmosPreference.addCustomStringPre(FusionPre.BACK_PASSWORD, "111111");
        }
        mBinding.etPassword.setText("");
        mBinding.etPassword.requestFocus();
    }

    private void onClick() {
        mBinding.btSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Objects.requireNonNull(mBinding.etPassword.getText()).toString().equals(AtmosPreference.getCustomStringPre(FusionPre.BACK_PASSWORD))) {
                    getSupportDelegate().pop();
                    getSupportDelegate().start(new BackHomeDelegate());
                } else {
                    showLongToast("密码错误");
                }
            }
        });
        mBinding.btCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getSupportDelegate().pop();
                EventBus.getDefault().post(new CheckPrintEvent(new Bundle()));
            }
        });

        mBinding.btClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clearNums();
            }
        });

        mBinding.btDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteOneNum();
            }
        });

        mBinding.btNum0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("0");
            }
        });

        mBinding.btNum1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("1");
            }
        });

        mBinding.btNum2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("2");
            }
        });

        mBinding.btNum3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("3");
            }
        });

        mBinding.btNum4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("4");
            }
        });

        mBinding.btNum5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("5");
            }
        });

        mBinding.btNum6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("6");
            }
        });

        mBinding.btNum7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("7");
            }
        });

        mBinding.btNum8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("8");
            }
        });

        mBinding.btNum9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("9");
            }
        });
    }

    @SuppressLint("SetTextI18n")
    private void appendOneNum(String num) {
        if (mBinding.etPassword.hasFocus()) {
            mBinding.etPassword.setText(mBinding.etPassword.getText().toString() + num);
        }
    }

    private void clearNums() {
        if (mBinding.etPassword.hasFocus()) {
            mBinding.etPassword.setText("");
        }
    }

    private void deleteOneNum() {
        if (mBinding.etPassword.hasFocus()) {
            String phoneStr = mBinding.etPassword.getText().toString();
            if (!StringUtil.isEmpty(phoneStr)) {
                mBinding.etPassword.setText(phoneStr.substring(0, phoneStr.length() - 1));
            }
        }
    }
}
